package cn.zhuizikejithreeig.com.passenger.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import cn.zhuizikejithreeig.com.been.AutoInfoBeen;
import cn.zhuizikejithreeig.com.utils.GsonUtil;
import cn.zhuizikejithreeig.com.utils.ToastMake;
import okhttp3.Call;

/**
 * 微信登录页面
 *
 * @author kevin_chen 2016-12-10 下午19:03:45
 * @version v1.0
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private static final String APP_SECRET = "ced9655d8737edbd5cd175cf8b424b1d";
    private IWXAPI mWeixinAPI;
    public static final String WEIXIN_APP_ID = "wxff391d4054eaf2c1";
    private static String uuid;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mWeixinAPI = WXAPIFactory.createWXAPI(this, WEIXIN_APP_ID, true);
        mWeixinAPI.handleIntent(this.getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        mWeixinAPI.handleIntent(intent, this);//必须调用此句话
    }

    //微信发送的请求将回调到onReq方法
    @Override
    public void onReq(BaseReq req) {

    }

    //发送到微信请求的响应结果
    @Override
    public void onResp(BaseResp resp) {

        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:

                //发送成功
                SendAuth.Resp sendResp = (SendAuth.Resp) resp;
                if (sendResp != null) {
                    String code = sendResp.code;
                    getAccess_token(code);
                }
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:

                //发送取消
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:

                //发送被拒绝
                break;
            default:
                //发送返回
                break;
        }

    }

    /**
     * 获取openid accessToken值用于后期操作
     *
     * @param code 请求码
     */
    private void getAccess_token(final String code) {
        String path = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
                + WEIXIN_APP_ID
                + "&secret="
                + APP_SECRET
                + "&code="
                + code
                + "&grant_type=authorization_code";
        OkHttpUtils
                .get()
                .url(path)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastMake.showToast("授权失败,请重新授权");
                        finish();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        AutoInfoBeen been = GsonUtil.parseJsonToBean(response, AutoInfoBeen.class);
                        if (been == null) {
                            ToastMake.showToast("授权失败,请重新授权");
                            finish();
                            return;
                        } else {
                            getUserMesg(been.getAccess_token(), been.getOpenid());
                        }
                    }
                });
    }


    /**
     * 获取微信的个人信息
     *
     * @param access_token
     * @param openid
     */
    private void getUserMesg(final String access_token, final String openid) {
        String path = "https://api.weixin.qq.com/sns/userinfo?access_token="
                + access_token
                + "&openid="
                + openid;
        OkHttpUtils
                .get()
                .url(path)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        System.out.println("获取用户信息失败");
                        finish();
                    }
                    @Override
                    public void onResponse(String response, int id) {
                        System.out.println("获取用户信息成功");
                        System.out.println(response);
                        System.out.println("获取用户信息成功");
                        finish();
                    }
                });
    }
}